package net.adst.ui.mainframe.tabs.nanemTap;

import net.adst.resources.VariableNames;
import net.adst.searcher.Searcher;
import net.adst.util.DataManager;
import net.adst.util.GBC;
import sun.net.ResourceManager;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created with IntelliJ IDEA.
 * User: alekseipot
 * Date: 02.11.12
 * Time: 6:54
 * To change this template use File | Settings | File Templates.
 */
public class SearchNaemPanel extends JPanel {
    private JLabel needUpdateLabel;
    private JLabel namesTecLabel;
    private JComboBox namesTecCombo;
    private JLabel managerLabel;
    private JComboBox managerCombo;
    private JLabel customerLabel;
    private JComboBox customerCombo;
    private JButton searchButton;

    public SearchNaemPanel() {
        setLayout(new GridBagLayout());
        addSearchFields();
        addSearchButton();

    }

    private void addSearchFields() {
        Dimension dimForCombo = new Dimension(99, 20);
        needUpdateLabel = new JLabel("Для вывода результатов нажмите кнопку поиск");
        needUpdateLabel.setFont(new Font("Big", Font.PLAIN, 14));
        needUpdateLabel.setForeground(Color.RED);
        needUpdateLabel.setVisible(false);
        DataManager.setObject(VariableNames.UI_NAEM_NEED_UPDATE_LABEL, needUpdateLabel);
        add(needUpdateLabel, new GBC(0, 0, 3, 1).setInsets(0, 5).setAnchor(GBC.SOUTHWEST));
        namesTecLabel = new JLabel("Выберете ед. техники:");
        namesTecCombo = new JComboBox();
        namesTecCombo.setMaximumSize(dimForCombo);
        namesTecCombo.setPreferredSize(dimForCombo);
        namesTecCombo.setMinimumSize(dimForCombo);
        DefaultComboBoxModel namesTecModel = new DefaultComboBoxModel();
        namesTecCombo.setModel(namesTecModel);
        ActionListener needUpdateListner = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ((JLabel) DataManager.getObject(VariableNames.UI_NAEM_NEED_UPDATE_LABEL)).setVisible(true);
            }
        };

        namesTecCombo.addActionListener(needUpdateListner);
        DataManager.setObject(VariableNames.UI_NAEM_TECNAMECOMBO_MODEL, namesTecModel);
        DataManager.setObject(VariableNames.UI_NAEM_TECNAMECOMBO, namesTecCombo);
        add(namesTecLabel, new GBC(0, 1).setInsets(0, 5).setAnchor(GBC.SOUTHWEST));
        add(namesTecCombo, new GBC(0, 2).setInsets(3, 5).setAnchor(GBC.SOUTHWEST));

        managerLabel = new JLabel("Выберете менагера:");
        managerCombo = new JComboBox();
        DefaultComboBoxModel managerModel = new DefaultComboBoxModel();
        managerCombo.setModel(managerModel);
        managerCombo.setMaximumSize(dimForCombo);
        managerCombo.setPreferredSize(dimForCombo);
        managerCombo.setMinimumSize(dimForCombo);
        managerCombo.addActionListener(needUpdateListner);
        DataManager.setObject(VariableNames.UI_NAEM_MANAGERCOMBO_MODEL, managerModel);
        DataManager.setObject(VariableNames.UI_NAEM_MANAGERCOMBO, managerCombo);
        add(managerLabel, new GBC(1, 1).setInsets(0, 5).setAnchor(GBC.SOUTHWEST));
        add(managerCombo, new GBC(1, 2).setInsets(3, 5).setAnchor(GBC.SOUTHWEST));

        customerLabel = new JLabel("Выберете заказчика:");
        customerCombo = new JComboBox();
        DefaultComboBoxModel customerModel = new DefaultComboBoxModel();
        customerCombo.setModel(customerModel);
        customerCombo.setMaximumSize(dimForCombo);
        customerCombo.setPreferredSize(dimForCombo);
        customerCombo.setMinimumSize(dimForCombo);
        customerCombo.addActionListener(needUpdateListner);
        DataManager.setObject(VariableNames.UI_NAEM_CUSTOMERCOMBO_MODEL, customerModel);
        DataManager.setObject(VariableNames.UI_NAEM_CUSTOMERCOMBO, customerCombo);
        add(customerLabel, new GBC(2, 1).setInsets(0, 5).setAnchor(GBC.SOUTHWEST));
        add(customerCombo, new GBC(2, 2).setInsets(3, 5).setAnchor(GBC.SOUTHWEST));

    }

    private void addSearchButton() {
        searchButton = new JButton(new ImageIcon(ResourceManager.class.getResource("/images/search-icon.png")));
        searchButton.setFocusPainted(false);
        add(searchButton, new GBC(3, 1, 1, 3).setInsets(0, 5).setAnchor(GBC.SOUTHWEST));
        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ((JLabel) DataManager.getObject(VariableNames.UI_NAEM_NEED_UPDATE_LABEL)).setVisible(false);
                Searcher.searchNaem();
            }
        }
        );
    }
}
